//
//  RankUserInfoController.m
//  CoolTennisBall
//
//  Created by Coollang on 2017/7/28.
//  Copyright © 2017年 CoolLang. All rights reserved.
//
#import "RankUserInfoController.h"
#import "CoollangTennisBall-Swift.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+CM_ShowImage.h"
#import "RankUserinfoModel.h"

@interface RankUserInfoController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet DesignableImageView *headiconImg;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *userLocationButton;
@property (weak, nonatomic) IBOutlet UILabel *likesCountLabel;
@property (weak, nonatomic) IBOutlet DesignableButton *giveLikeButton;

@property (weak, nonatomic) IBOutlet UILabel *sportTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *sportDurationLabel;
@property (weak, nonatomic) IBOutlet UILabel *sportDurationDescLabel;

@property (weak, nonatomic) IBOutlet UILabel *swingTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingtimesDescLabel;

@property (weak, nonatomic) IBOutlet UILabel *swingWeeklyRankDescLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingWeeklyRankLabel;

@property (weak, nonatomic) IBOutlet UILabel *swingMonthRankDescLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingMonthRankLabel;

@property (weak, nonatomic) IBOutlet UILabel *swingRankTitleLabel;

@property (weak, nonatomic) IBOutlet UILabel *scoreWeeklyRankDescLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreWeeklyRankLabel;

@property (weak, nonatomic) IBOutlet UILabel *scoreMonthRankDescLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreMonthRankLabel;

@property (weak, nonatomic) IBOutlet UILabel *scoreRankTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *ceecerTitleLabel;

@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *medalImageviews;

@property (nonatomic, strong) RankUserinfoModel *rankInfoModel;

/** 是否正在点赞 */
@property (nonatomic, assign) BOOL islikeRequest;

@end

@implementation RankUserInfoController

+ (instancetype)rankUserInfoControllerWithRankInfoModel:(RankUserinfoModel *)rankInfoModel {
    RankUserInfoController *infoVc = [[UIStoryboard storyboardWithName:@"Ranking" bundle:nil] instantiateViewControllerWithIdentifier:@"RankUserInfoController"];
    infoVc.rankInfoModel = rankInfoModel;
    return infoVc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addGestureRecognizer];
    [self configureLocalization];
    [self updateUIWith:self.rankInfoModel];
    
    self.giveLikeButton.selected = self.isliked;
    
    [self.giveLikeButton setTitle:NSLocalizedString(@"Rank.Cancel Like", @"取消点赞") forState:UIControlStateSelected];
    [self.giveLikeButton setTitle:NSLocalizedString(@"Rank.Like", @"给他点赞") forState:UIControlStateNormal];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHeadImage)];
    self.headiconImg.userInteractionEnabled = YES;
    [self.headiconImg addGestureRecognizer:tap];
}

- (void)showHeadImage {
    [self.headiconImg showImage];
}

- (void)configureLocalization {
    self.sportTitleLabel.text = NSLocalizedString(@"Exercise", @"运动");

    self.sportDurationDescLabel.text = NSLocalizedString(@"Exercise time(h)", nil);
    
    self.swingtimesDescLabel.text = NSLocalizedString(@"Rank.Shots", "挥拍次数");
    self.swingRankTitleLabel.text = NSLocalizedString(@"Rank.Swing", nil);
    NSString *weeklyStr = NSLocalizedString(@"Weekly Ranking", nil);
    NSString *monthlyStr = NSLocalizedString(@"Monthly Ranking", nil);
    if([NSBundle currentLangeuage] == kLanguageFrennch){
        weeklyStr = [weeklyStr stringByReplacingOccurrencesOfString:@" Classement" withString:@""];
        monthlyStr = [monthlyStr stringByReplacingOccurrencesOfString:@"Classement" withString:@""];
    }
    
    self.swingWeeklyRankDescLabel.text = weeklyStr;
    self.swingMonthRankDescLabel.text = monthlyStr;
    
    self.scoreWeeklyRankDescLabel.text = weeklyStr;
    self.scoreMonthRankDescLabel.text = monthlyStr;
    self.ceecerTitleLabel.text = NSLocalizedString(@"Career", @"生涯");
    self.scoreRankTitleLabel.text = NSLocalizedString(@"Rank.Score",nil);
    
}

- (void)updateUIWith:(RankUserinfoModel *)rankInfoModel {
    
    [self.headiconImg sd_setImageWithURL:[NSURL URLWithString:rankInfoModel.headIcon] placeholderImage:[UIImage imageNamed:@"competition_icon_defaulImage"] options:SDWebImageRetryFailed | SDWebImageLowPriority];
    
    self.nameLabel.text = rankInfoModel.userName;
   
    if (rankInfoModel.adress.length > 0){
        [self.userLocationButton setImage:[UIImage imageNamed:@"rank_location"] forState:UIControlStateNormal];
         [self.userLocationButton setTitle:[NSString stringWithFormat:@" %@",rankInfoModel.adress] forState:UIControlStateNormal];
    }else {
        [self.userLocationButton setImage:nil forState:UIControlStateNormal];
        [self.userLocationButton setTitle:nil forState:UIControlStateNormal];
    }
    self.likesCountLabel.text = rankInfoModel.likes;
    
    self.sportDurationLabel.text = rankInfoModel.totalDuration;
    self.swingTimeLabel.text = rankInfoModel.totalSwings;
    self.swingWeeklyRankLabel.text = rankInfoModel.swingWeekRank;
    self.swingMonthRankLabel.text = rankInfoModel.swingMonthRank;
    self.scoreWeeklyRankLabel.text = rankInfoModel.scoreWeekRank;
    self.scoreMonthRankLabel.text = rankInfoModel.scoreMonthRank;
    
    [self showMedalIconWithMedalItems:rankInfoModel.medals];
}

- (void)configureDefaultMedalIcons {
    for (UIImageView *medalIcon in self.medalImageviews) {
        if (medalIcon.tag - 70 < 6) {
            medalIcon.image = [UIImage imageNamed: [NSString stringWithFormat:NSLocalizedString(@"medal_icon_%d_en", nil), medalIcon.tag - 70]];
        }
    }
}

- (void)showMedalIconWithMedalItems:(NSArray *)medalItems
{
    NSLog(@"%@", medalItems);
    NSMutableArray *medalIconIndexs = @[].mutableCopy;
    
    for (NSInteger index = 0; index < [self.medalImageviews count]; index++) {
        [medalIconIndexs addObject:@(index)];
    }
    
    for (NSInteger index = 0; index < [medalItems count]; index++) {
        UIImageView *medalIcon = [self medalIconWithTag:index];
        NSInteger typeIndex = [medalItems[index] integerValue];
        medalIcon.image = [UIImage imageNamed: [NSString stringWithFormat:NSLocalizedString(@"medal_icon_get_%d_en", nil), typeIndex]];
        if ([NSBundle currentLanguageIsChinese]) {
           
        }
        
        if([medalIconIndexs containsObject:@(typeIndex)]) {
            [medalIconIndexs removeObject:@(typeIndex)];
        }
    }
    
    for (NSInteger index = [medalItems count]; index < [self.medalImageviews count]; index++) {
        UIImageView *medalIcon = [self medalIconWithTag:index];
        NSInteger medalIndex = [medalIconIndexs[index - [medalItems count]] integerValue];
        // 隐藏icon需要注意是否显示默认
        NSInteger hideMedalIconIndex = [self.medalImageviews count] - 3;
        if (index < hideMedalIconIndex|| (index >= hideMedalIconIndex && medalIndex < hideMedalIconIndex)) {
            medalIcon.image = [UIImage imageNamed: [NSString stringWithFormat:NSLocalizedString(@"medal_icon_%d_en", nil), medalIndex]];
        }
    }
}

- (UIImageView *)medalIconWithTag:(NSInteger)tag {
    
    for (UIImageView *medalIcon in self.medalImageviews) {
        if (medalIcon.tag - 70 == tag) {
            return medalIcon;
        }
    }
    return nil;
}

#pragma mark - Private Method
- (IBAction)giveLikeButtonClick:(UIButton *)sender {
    
    if (self.islikeRequest != NO || self.likeButtonClickBlock == nil) {
        return;
    }
    self.islikeRequest = YES;
    
    self.likeButtonClickBlock(self.isliked, ^(BOOL success) {
        self.islikeRequest = NO;
        if (success){
            self.isliked = !self.isliked;
            sender.selected = self.isliked;
            NSInteger change = self.isliked ? 1:-1;
            self.rankInfoModel.likes = @(self.rankInfoModel.likes.integerValue + change).stringValue;
            self.likesCountLabel.text = self.rankInfoModel.likes;
        }
    });
    
}

- (void)addGestureRecognizer {
    
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClose:)];
    gestureRecognizer.cancelsTouchesInView = NO;
    gestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:gestureRecognizer];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return (touch.view == self.view);
}

- (void)tapClose:(UITapGestureRecognizer *)tapGes
{
     [self dismissFromParentViewControllerWithAnimationType:DetailViewControllerAnimationTypeFade];
}

@end
